<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {

	function __construct() {
		// Need to run CI_Controller constructor
		parent::__construct();
		// Load the user model
		// This is needed for checking user is logged in and has permission to access the page
		$this->load->model('user');
	}

	/**
	 * Account management homepage
	 */
	public function index() {

		// Check if the user is logged in
		if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }

		$profile = $this->user->get_user_data( $this->session->userdata('user_id') );

		// Page title to send to view
		$data['page_title'] = "My Account";
		// Page content 
		$data['main_content'] = "account/account_home";
		// Information about the user
		$data['profile'] = $profile;

		$data['content_title'] = 'Your Account';
		$data['left_sidebar'] = 'account';
		$data['sidebar_modifier'] = 'Hide Right';
		$data['left_sidebar_title'] = 'Update Account';

		// load the view
		$this->load->view('template', $data);
	}

	/**
	 * Change the users password
	 */
	public function change_password() {

		// Check if the user is logged in
		if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }

		if ( $this->input->post() ) {
			// Get the form data
			$post = $this->input->post();
			// Check if the two passwords match
			if ( $post['new_password'] == $post['confirm_new_password'] ) {
				// Change the password
				$this->user->change_password( $this->session->userdata('user_id') , $post['new_password'] );
			} else { 
				$this->session->set_flashdata('errors', 'Passwords do not match. Please try again.' );
			}
			redirect('account/change_password');
		}

		// Page title to send to view
		$data['page_title'] = "My Account";
		// Page content 
		$data['main_content'] = "account/change_password";

		$data['content_title'] = 'Change Your Password';
		$data['left_sidebar'] = 'account';
		$data['sidebar_modifier'] = 'Hide Right';
		$data['left_sidebar_title'] = 'Update Account';

		// load the view
		$this->load->view('template', $data);
	}


	/**
	 * Change email address
	 */
	public function change_email() {

		// Check if the user is logged in
		if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }

		if ( $this->input->post() ) {
			// Get the form data
			$post = $this->input->post();

			// Check if the two email addresses
			if ( $post['new_email'] == $post['confirm_new_email'] ) {
				// TODO: validate email address
				// Change the password
				$this->user->change_email( $this->session->userdata('user_id') , $post['new_email'] );
			} else {
				$this->session->set_flashdata('errors', 'Email addresses do not match. Please try again.' );
			}
			redirect('account/change_email');
		}

		// Page title to send to view
		$data['page_title'] = "My Account";
		// Page content 
		$data['main_content'] = "account/change_email";

		$data['content_title'] = 'Change Your Email Address';
		$data['left_sidebar'] = 'account';
		$data['sidebar_modifier'] = 'Hide Right';
		$data['left_sidebar_title'] = 'Update Account';

		// load the view
		$this->load->view('template', $data);
	}


}

/* End of file account.php */
/* Location: ./application/controllers/account.php */